<template>
  <div class="_wz_cent_main _wz_dataDict">
    <Card :bordered="true" style="margin:0 0 10px;">
      <p slot="title">搜索</p>
      <Form :model="formDataSearch" label-position="right" :label-width="100" inline @keydown.native.enter.prevent="">
        <FormItem label="字典类型">
          <i-select v-model="formDataSearch.type" filterable placeholder="请选择字典类型" clearable>
            <Option :value="item.code" :key="item.id" v-for="item in companyDict">{{ item.value }}</Option>
          </i-select>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="searchSubmit">搜索</Button>
        </FormItem>
      </Form>
    </Card>
    <Card dis-hover :bordered="true">
      <p slot="title">列表</p>
      <p slot="extra">
        <Button type="primary" @click="addTableData">添加</Button>
      </p>
      <wzTbale ref="wzTbale" :tableData="tableData"></wzTbale>
    </Card>

    <Drawer :title="formTitle" v-model="formOperation" width="720" :draggable="true" :mask-closable="false" :styles="formOperationStyles">
        <dataDictAdd v-if="formOperation" :selectData="selectData"  @closeFun="closeFun"></dataDictAdd>
    </Drawer>
    <Spin size="large" fix v-if="spinShow"></Spin>

  </div>
</template>
<script>
import wzTbale from "../../components/table/table";
import dataDictAdd from "./dataDictAdd";
  export default {
    components: {
      wzTbale,//列表
      dataDictAdd,
    },
    data() {
      return {
        spinShow:false,//本页加载
        formDataSearch: {},//搜索表单数据
        selectData: {},
        formTitle: "",//表单名称
        companyDict:[],//企业字典
        formOperation: false,//表单左侧移出
        formOperationStyles: {//表单左侧移出css
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          paddingBottom: '53px',
          position: 'static'
        },
        tableData: {
          url: this.apiUrl.coreDeptDictCoreDeptDictPageList,//请求地址
          method: 'post',//请求方式
          where: {},//搜索条件
          height: 'auto',//表格高度 auto
          page: true,//分页是否使用
          columns: [//表格表头数据
            {type: 'index', width: 80, align: 'center', title: '序号'},
            {title: '字典数值', minWidth: 300, key: 'value'},
            {title: '字典类型', minWidth: 300, key: 'name'},
            {
              title: '操作', fixed: 'right',width: 180, render: (h, params) => {
                let operationButtons = [];
                //修改
                operationButtons.push(
                  h('span', {
                    style: {"color": "#2d8cf0", "cursor": "pointer"},
                    on: {
                      click: () => {
                        this.formTitle = '修改';
                        this.selectData = JSON.parse(JSON.stringify(params.row));
                        this.formOperation = true;
                      }
                    }
                  }, '修改')
                );

                //删除
                if (operationButtons.length > 0) {
                  operationButtons.push(h('Divider', {props: {type: 'vertical',}}, ''))
                }
                operationButtons.push(
                  h('span', {
                    style: {"color": "#2d8cf0", "cursor": "pointer"},
                    on: {
                      click: () => {
                        let that = this;
                        that.$Modal.confirm({
                          title: '删除提醒',
                          content: '确定要删除选中的记录？',
                          onOk: () => {
                            that.spinShow = true;//显示加载
                            that.axios.post(that.apiUrl.coreDeptDictCoreDeptDictDelete, {uuid: params.row.uuid}).then(res => {
                              that.spinShow = false;
                              if (!!res) {
                                that.$Message.success('数据删除成功.');
                                that.$refs.wzTbale.tableLoadData();
                              }
                            }).catch(err => {
                              console.log("失败", err)
                            })
                          },
                          onCancel: () => {
                            //点击取消
                          }
                        });
                      }
                    }
                  }, "删除")
                );
                return h('div', operationButtons);
              }
            }
          ],
          format: {//返回数据格式
            list: ['data'],//数据list
            count: ['count'],//数据总量
          },
        },
        //验证
        ruleValidate: {
          type: [
            {required: true, message: '请选择字典类型', trigger: 'blur'},
          ],
          value: [
            {required: true, message: '请填写字典数值', trigger: 'blur'},
          ],
        },
      }
    },
    beforeCreate() {//beforeCreate创建前状态

    },
    created() {//created创建完毕状态
    },
    beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

    },
    mounted() {//mounted 挂载结束状态
      var that = this;
      //企业字典
      that.axios.post(that.apiUrl.systemDictSystemDictList, {type: "company_dict"}).then(res => {
        if (!!res) {
          that.companyDict = res.data.data;
        }
      }).catch(err => {
        console.log("失败", err)
      })

    },
    methods: {//执行的方法
      addTableData() {//table添加
        this.formTitle = "添加";
        this.formData = {};
        this.formOperation = true;
      },
      searchSubmit() {//搜索
        this.tableData.where = JSON.parse(JSON.stringify(this.formDataSearch));
        this.$refs.wzTbale.tableLoadData(true);
      },
      //添加修改触发后关闭刷新
      closeFun(data) {
        var that = this;
        if (data) {//添加修改 刷新表单
          that.$refs.wzTbale.tableLoadData();
        }
        that.formOperation = false;
      },
    },
    watch: {//监听

    },
    props: {//接收来自父组件的数据

    },
    beforeUpdate() {//beforeUpdate 更新前状态

    },
    updated() {//updated 更新完成状态

    },
    beforeDestroy() {//beforeDestroy 销毁前状态

    },
    destroyed() {//destroyed 销毁完成状态

    }
  }
</script>
<style lang="less">
  ._wz_dataDict {
    display: flex;
    flex-direction: column;
  }
</style>
